using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using Mussel;
using Mussel.Data;
using Natrue.Data;
using Natrue.Data.FastMapping;
using Natrue.Popedom.Menu;
using Natrue.Popedom.Organizational;

namespace Natrue.Web.MVC
{
    /// <summary>
    /// Nature ViewPage Base
    /// </summary>
    public class AuthViewPage : ViewPage, IViewMethods
    {
        private readonly IViewMethods pageBase = ViewMethods.Instanct;

        #region Implementation of IPageBase

        public ILocalAddinItemProxyContainer ProxyContainer
        {
            get { return pageBase.ProxyContainer; }
        }

        public IEnumHelper EnumHelper
        {
            get { return pageBase.EnumHelper; }
        }

        public ISmartView SmartView
        {
            get { return pageBase.SmartView; }
        }

        public ISystemMenu SystemMenu
        {
            get { return pageBase.SystemMenu; }
        }

        public string ApplicationName
        {
            get { return pageBase.ApplicationName; }
        }

        public string BuildSearchConditionControls(Type iboType, string smartListView)
        {
            return pageBase.BuildSearchConditionControls(iboType, smartListView);
        }

        public IList<SelectListItem> BuildEnumSelectListItem<T>()
        {
            return pageBase.BuildEnumSelectListItem<T>();
        }

        public IList<SelectListItem> BuildEnumSelectListItem<T>(T selectValue)
        {
            return pageBase.BuildEnumSelectListItem(selectValue);
        }

        public IList<SelectListItem> BuildEnumSelectListItem<T>(string selectValue)
        {
            return pageBase.BuildEnumSelectListItem<T>(selectValue);
        }

        public IList<SelectListItem> BuildEnumSelectListItem(Type enumType)
        {
            return pageBase.BuildEnumSelectListItem(enumType);
        }

        public IList<SelectListItem> BuildEnumSelectListItem(Type enumType, object selectValue)
        {
            return pageBase.BuildEnumSelectListItem(enumType, selectValue);
        }

        public IList<SelectListItem> BuildEnumSelectListItem(Type enumType, string selectValue)
        {
            return pageBase.BuildEnumSelectListItem(enumType, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem<T>(string caption) where T : IBO
        {
            return pageBase.BuildBelongsToSelectListItem<T>(caption);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem<T>(string caption, T selectValue) where T : IBO
        {
            return pageBase.BuildBelongsToSelectListItem(caption, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem<T>(string caption, Guid selectValue) where T : IBO
        {
            return pageBase.BuildBelongsToSelectListItem<T>(caption, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem(Type belongsToType, string caption)
        {
            return pageBase.BuildBelongsToSelectListItem(belongsToType, caption);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem(Type belongsToType, string caption, IBO selectValue)
        {
            return pageBase.BuildBelongsToSelectListItem(belongsToType, caption, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem(Type belongsToType, string caption, Guid selectValue)
        {
            return pageBase.BuildBelongsToSelectListItem(belongsToType, caption, selectValue);
        }

        #endregion

        protected User CurrentUser
        {
            get
            {
                HttpCookie userId = Request.Cookies["currentUserId"];
                if (userId == null) return null;
                Guid userGuid = new Guid(userId.Value);
                IPermissionOperation op = ProxyContainer.FindLocalProxy<IPermissionOperation>();
                return op.FindUserdetail(userGuid);
            }
        }

        protected Employee CurrentEmployee
        {
            get
            {
                if (CurrentUser == null) return null;
                IOrganizationalManager orgMgr = ProxyContainer.FindLocalProxy<IOrganizationalManager>();
                return orgMgr.GetEmployeeDetail(CurrentUser.UserName);
            }
        }

        protected DepartmentBase[] CurrentEmployeeDepartments
        {
            get
            {
                if (CurrentEmployee == null) return null;
                IOrganizationalManager orgMgr = ProxyContainer.FindLocalProxy<IOrganizationalManager>();
                DepEmpRel[] rels = orgMgr.GetDepEmpRelDetail(CurrentEmployee);
                List<DepartmentBase> reVals = new List<DepartmentBase>();
                foreach (DepEmpRel empRel in rels)
                    reVals.Add(orgMgr.GetDepartmentBaseDetail(empRel.Department.PrimaryKey));
                return reVals.ToArray();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

    /// <summary>
    /// Nature ViewPage Base
    /// </summary>
    /// <typeparam name="Model"></typeparam>
    public class AuthViewPage<Model> : ViewPage<Model>, IViewMethods where Model : class
    {
        private readonly IViewMethods pageBase = ViewMethods.Instanct;

        #region Implementation of IPageBase

        public ILocalAddinItemProxyContainer ProxyContainer
        {
            get { return pageBase.ProxyContainer; }
        }

        public IEnumHelper EnumHelper
        {
            get { return pageBase.EnumHelper; }
        }

        public ISmartView SmartView
        {
            get { return pageBase.SmartView; }
        }

        public ISystemMenu SystemMenu
        {
            get { return pageBase.SystemMenu; }
        }

        public string ApplicationName
        {
            get { return pageBase.ApplicationName; }
        }

        public string BuildSearchConditionControls(Type iboType, string smartListView)
        {
            return pageBase.BuildSearchConditionControls(iboType, smartListView);
        }

        public IList<SelectListItem> BuildEnumSelectListItem<T>()
        {
            return pageBase.BuildEnumSelectListItem<T>();
        }

        public IList<SelectListItem> BuildEnumSelectListItem<T>(T selectValue)
        {
            return pageBase.BuildEnumSelectListItem(selectValue);
        }

        public IList<SelectListItem> BuildEnumSelectListItem<T>(string selectValue)
        {
            return pageBase.BuildEnumSelectListItem<T>(selectValue);
        }

        public IList<SelectListItem> BuildEnumSelectListItem(Type enumType)
        {
            return pageBase.BuildEnumSelectListItem(enumType);
        }

        public IList<SelectListItem> BuildEnumSelectListItem(Type enumType, object selectValue)
        {
            return pageBase.BuildEnumSelectListItem(enumType, selectValue);
        }

        public IList<SelectListItem> BuildEnumSelectListItem(Type enumType, string selectValue)
        {
            return pageBase.BuildEnumSelectListItem(enumType, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem<T>(string caption) where T : IBO
        {
            return pageBase.BuildBelongsToSelectListItem<T>(caption);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem<T>(string caption, T selectValue) where T : IBO
        {
            return pageBase.BuildBelongsToSelectListItem(caption, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem<T>(string caption, Guid selectValue) where T : IBO
        {
            return pageBase.BuildBelongsToSelectListItem<T>(caption, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem(Type belongsToType, string caption)
        {
            return pageBase.BuildBelongsToSelectListItem(belongsToType, caption);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem(Type belongsToType, string caption, IBO selectValue)
        {
            return pageBase.BuildBelongsToSelectListItem(belongsToType, caption, selectValue);
        }

        public IList<SelectListItem> BuildBelongsToSelectListItem(Type belongsToType, string caption, Guid selectValue)
        {
            return pageBase.BuildBelongsToSelectListItem(belongsToType, caption, selectValue);
        }

        #endregion

        protected User CurrentUser
        {
            get
            {
                HttpCookie userId = Request.Cookies["currentUserId"];
                if (userId == null) return null;
                Guid userGuid = new Guid(userId.Value);
                IPermissionOperation op = ProxyContainer.FindLocalProxy<IPermissionOperation>();
                return op.FindUserdetail(userGuid);
            }
        }

        protected Employee CurrentEmployee
        {
            get
            {
                if (CurrentUser == null) return null;
                IOrganizationalManager orgMgr = ProxyContainer.FindLocalProxy<IOrganizationalManager>();
                return orgMgr.GetEmployeeDetail(CurrentUser.UserName);
            }
        }

        protected DepartmentBase[] CurrentEmployeeDepartments
        {
            get
            {
                if (CurrentEmployee == null) return null;
                IOrganizationalManager orgMgr = ProxyContainer.FindLocalProxy<IOrganizationalManager>();
                DepEmpRel[] rels = orgMgr.GetDepEmpRelDetail(CurrentEmployee);
                List<DepartmentBase> reVals = new List<DepartmentBase>();
                foreach (DepEmpRel empRel in rels)
                    reVals.Add(orgMgr.GetDepartmentBaseDetail(empRel.Department.PrimaryKey));
                return reVals.ToArray();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

}